<include file="Common:header"/>
<style>
.progress{position:relative; margin-top:45px;width:200px;padding: 1px; border-radius:3px; display:block}
.bar {background-color: green; display:block; width:0%; height:20px;border-radius:3px; }
.botton_s{float:left;width:49%;text-align:center;height:40px;line-height:40px;font-size:17px;border-right:1px solid #DADADA;}
.size{width:50%;height:120px;float:left;}
.input_t{height:30px;float:right;width:75%;margin:0px;border:none;margin-top:1px;border-radius:0px;}
.p_class{float:left;width:23%;height:34px;line-height:34px;text-align:center}
.word_style{border-top:1px solid #DADADA;border-bottom:1px solid #DADADA;width:100%;height:34px;margin:0px;margin-top:20px}
.up_img{margin:0px;margin-top:20px;width:100%;border-top:1px solid #DADADA;border-bottom:1px solid #DADADA;height:120px}
.up_img_p{text-align:center;line-height:120px;font-size:35px;width:50%;float:right;height:120px;}
.img_div{width:60%;height:150px;border:1px solid #DADADA;margin:0 auto;position:absolute;top:0px;left:23%;background-color:white}
.upload{width:100%;height:110px;border-bottom:1px solid #DADADA;text-align:center;line-height:110px;font-size:25px;}
</style>
<body>
	<div class="edit_con">
		<div class="bannerbaowei">
			<div class="dajianxi"></div>
			<div class="tit1h4391">上传素材</div>
		</div>
		<div class="edit_area">
			<form enctype="multipart/form-data" id="form">
				<div class="word_style" >
					<p class="p_class">标题：</p>
					<input type="text"  name="title" class="input_t" value="<{$ad['title']}>" >
				</div>

				<div class="clear"></div>
				
				<div class="word_style">
					<p class="p_class">上传类型：</p>
					<select class="input_t" name="type" style="height:32px" >
						<option value="1" <if condition="$ad['type'] eq 1"> selected="selected" </if>>文章</option>
						<option value="2" <if condition="$ad['type'] eq 2"> selected="selected" </if>>图片</option>
						<option value="3" <if condition="$ad['type'] eq 3"> selected="selected" </if>>视频</option>
					</select>
				</div>

				<div style="position:relative">

				<div class="clear"></div>

				<div class="word_style" id="line" >
					<p class="p_class">链接地址：<!-- （点击广告跳转到此地址） --></p>
					<input type="text" name="upload_link" class="input_t"  value='<{$ad['upload_link']}>' onchange="display(this)">
				</div>

				<div class="clear"></div>
		
				<div class="up_img" style="display:none">
						<!-- 用来激活上传图片插件 -->
						<botton class="botton_s"  id="select_dom" style="display:none">上传图片</botton>
						<div class="size">
							<img id="show_img"  src="/<{$ad['upload_address']}>" style="width:100%;height:100%">
						</div>
						<p class="up_img_p">上传图片</p>
						<{:A('Upload')->upload_tool('select_dom','show_img','true')}>
				</div>

				<div class="clear"></div>
				<div class="eing" style="padding:10px;margin-top:10px">
					<if condition="$ad['type'] == 1 || $ad['type'] == 3">
						<iframe src="<{$ad['upload_link']}>" frameborder="0" allowfullscreen="" style="height:300px;width:100%;"></iframe>
					</if>
				</div>
				<div class="clear"></div>
				
				<div class="btn_area" style="margin-bottom:50px">
					<input style="width:100%" type="button" onclick="commit(this)" class="banner_sub" value="提交" style="text-align:center">
					<input type="hidden" value="<{$ad['id']}>" name="id" >
					<input type="hidden" id ="upload" name="upload_address" value="<{$ad['upload_address']}>">
				</div>
			</form>
		</div>
		<include file="Common:menu"/>
	</div>
</body>
</html>
<script>
	$(function(){
		var type_val = "<{$ad['type']}>";
		
		if(type_val == ''){
			$("#line").show();
			$(".up_img").hide();
			return false;
		}

		if(type_val == 1){
			$("#line").show();
			$(".up_img").hide();
			return false;
		}

		if(type_val == 2){
			$("#line").hide();
			$(".up_img").show();
			return false;
		}

		if(type_val == 3){
			$("#line").show();
			$(".up_img").show();
			return false;
		}
		$("iframe").css({
			"width": "100%",
			"height": "300px"
		});
	})
	
	
	//显示图片
	$(".up_img_p").click(function(){
		layer.open({
		    content: '请选择上传图片方式',
		    btn: ['上传图片'],
		    shadeClose: true,
		    yes: function(){
		    	$("#select_dom").click();
		        layer.closeAll();
		    }, no: function(){
		    	$(".text_library").show();
		        layer.closeAll();
		    }
		});
		return false;
	})
	
	
	//切换模式
	$("select[name='type']").change(function(){
		var type_val = $(this).val();

		//文章
		if(type_val == 1){
			$('.eing').html('');
			$("#line").show();
			$(".up_img").hide();
			$('#show_img').attr('src','');
			$('input[name="upload_link"]').val('');
			$('#upload_address').val('');
			return false;
		}

		//tupian
		if(type_val == 2){
			$('.eing').html('');
			$("#line").hide();
			$(".up_img").show();
			$('#show_img').attr('src','');
			$('input[name="upload_link"]').val('');
			$('#upload_address').val('');
			return false;
		}

		//视频
		if(type_val == 3){
			
			$('.eing').html('');
			$("#line").show();
			$(".up_img").show();
			$('#show_img').attr('src','');
			$('input[name="upload_link"]').val('');
			$('#upload_address').val('');
			return false;
		}
		
	});

	
	//页面显示
	function display(obj){
	
		var type        = $("select[name='type']").val();
		var upload_link = $(obj).val();
		var tvideo_url = "<{:U('Home/Article/display_iframe')}>";
		
		if(upload_link == '' || upload_link == undefined){
			$('.eing').html('');
			return false;
		}
		
		//视频
		if(type == '3'){
			$.ajax({
				url: tvideo_url,
				data: {
					"url": upload_link
				},
				dataType: "json",
				async:false,
				success: function(data){
					var videocode = data.url;
				
					if (data.error) {
						$('input[name="upload_link"]').val(videocode);
						var html = '<iframe height="480" width="400" src="'+videocode+'" frameborder="0" allowfullscreen=""></iframe>';
						$(".eing").append(html);
						$("iframe").css({
							"width": "100%",
							"height": "300px"
						});
						return false;
					} else {
						layer.open({
							content:'该视频地址无效，请重新粘贴！'
						});
						return false;
					}
				}
			});
		}else if(type == '1'){
			//文章
			var html = '<iframe height="480" width="400" src="'+upload_link+'" frameborder="0" allowfullscreen=""></iframe>';
			$(".eing").append(html);
			$("iframe").css({
				"width": "100%",
				"height": "300px"
			});
			
			return false;
			
		}else{
			layer.open({
				content:'非法操作！'
			});
			return false;
		}
	}
	
	//提交
	function commit(obj){
	
		var title  = $('input[name="title"]').val();
		var type   = $("select[name='type']").val();
		var upload = $('#show_img').attr('src');
		var link   = $('input[name="upload_link"]').val();
		
		if(type == '1' || type == '3'){
			if(link == '' || link == undefined){
				layer.open({
					content:'请输入链接',
				});
				return false;
			}
		}else if(type == '2'){
			if(upload == '' || upload == undefined){
				layer.open({
					content:'请上传图片',
				});
				return false;
			}
		}else{
			layer.open({
				content:'非法参数',
			});
			return false;
		}
		
		if($(obj).attr('readonly')){
			return false;
		}

		$(obj).attr('readonly','readonly');
		$.ajax({
		   type: "POST",
		   url: "<{:U('Home/Material/ajax_upload_material')}>",
		   data: $('#form').serialize(),
		   success: function(msg){
				if(msg.status == '1'){
					layer.open({
						content:'上传成功',
						btn: ['确认'],
						yes:function(){
						   window.location.href = '<{:U("Home/Material/upload_material_list")}>';
						}
					});
			    }else{
					layer.open({
						content:msg.msg
					});
					$(obj).removeAttr('readonly');
					return false;
				}
				
		   }
		});
	}

	
	//清除链接
	function clear_link(){
		$('input[name="url"]').val('');
		return false;
	}

	
	//清楚图片
	function clear_picture(obj){
		$('#upload_picture').val('');
		$(obj).parent().find('img').attr('src','');
		return false;
	}


</script>
